package com.goldmantis.alitrip.controller;

import com.alibaba.fastjson.JSON;
import com.goldmantis.alitrip.pojo.AlitripBtripApprovalModel;
import com.goldmantis.alitrip.pojo.AlitripBtripApprovalRequestModel;
import com.goldmantis.alitrip.service.ApprovalNewService;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.request.AlitripBtripApprovalNewRequest;
import com.taobao.api.request.AlitripBtripApprovalNewRequest.OpenItineraryInfo;
import com.taobao.api.response.AlitripBtripApprovalNewResponse;
import com.taobao.api.response.AlitripBtripApprovalUpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 17:55 2018/1/15
 */
@Controller
@RequestMapping("approval/new/")
@CrossOrigin(value = "*",maxAge = 3600)
public class ApprovalNewController {

    private static final Logger logger = LoggerFactory.getLogger(ApprovalNewController.class);

    @Autowired
    private ApprovalNewService approvalNewService;


    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResponseEntity<AlitripBtripApprovalModel> insertNewApproval(@RequestBody AlitripBtripApprovalRequestModel request) throws Exception {

        AlitripBtripApprovalNewRequest.OpenAddApplyRq applyRequest =request.getApplyRequest();
        List<AlitripBtripApprovalNewRequest.OpenItineraryInfo> itineraryInfoList=request.getItineraryInfoList();
        List<AlitripBtripApprovalNewRequest.OpenUserInfo> travelerList=request.getTravelerList();

        try {

            if(logger.isInfoEnabled()){
                logger.info("收到数据:"+ JSON.toJSONString(applyRequest));
            }

            //如果未传递成本中心，系统则会给默认成本中心。
            for(OpenItineraryInfo item :itineraryInfoList){
                if(item.getCostCenterId().longValue()==0L){
                    item.setCostCenterId(3046L); //苏州金螳螂建筑装饰股份有限公司
                }
            }

            AlitripBtripApprovalModel rsp = approvalNewService.insertNewApproval(applyRequest, itineraryInfoList, travelerList);
            if(rsp==null){
                logger.info(applyRequest.getApplyId() + "单据审批失败" + JSON.toJSONString(rsp));
            }

            if (logger.isInfoEnabled()) {
                System.out.println(JSON.toJSONString(rsp));
                logger.info(applyRequest.getApplyId() + "单据同步成功:" + JSON.toJSONString(rsp));
            }

            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(applyRequest.getApplyId() + "单据提交失败" + JSON.toJSONString(applyRequest));
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 测试数据
     * @return
     */
    @RequestMapping(value = "insertTest", method = RequestMethod.POST)
    public ResponseEntity<AlitripBtripApprovalModel> insertNewApprovalTest()
    {
        try {
            //初始化行程列表
            List<OpenItineraryInfo> itineraryInfoList = new ArrayList<>();

            OpenItineraryInfo openItineraryInfo2 = new OpenItineraryInfo();
            openItineraryInfo2.setArrCity("北京"); //到达城市
            openItineraryInfo2.setArrCityCode("BJS"); //可选：到达城市编码

            openItineraryInfo2.setDepCity("苏州"); //到达城市
            openItineraryInfo2.setDepCityCode("SZV");  //可选：出发城市编码
            openItineraryInfo2.setTrafficType(0L);  //交通方式：0飞机, 1,火车, 2汽车, 3其他
            openItineraryInfo2.setItineraryId("金螳螂行程单itineraryId" + new Random().nextInt(99999));
            openItineraryInfo2.setTripWay(1L); //行程类型：0单程，1往返
            openItineraryInfo2.setArrDate(StringUtils.parseDateTime("2018-03-19 00:00:00")); //到达日期
            openItineraryInfo2.setDepDate(StringUtils.parseDateTime("2018-03-17 00:00:00")); //出发日期
            openItineraryInfo2.setInvoiceId(4969L); //苏州金螳螂建筑装饰股份有限公司
            openItineraryInfo2.setCostCenterId(103271L);  //可选：苏州金螳螂建筑装饰股份有限公司
            openItineraryInfo2.setThirdpartCostCenterId("第三方成本中心id，若不填则商旅成本中心id必填");  //第三方成本中心id，若不填则商旅成本中心id必填
            itineraryInfoList.add(openItineraryInfo2);

            OpenItineraryInfo openItineraryInfo = new OpenItineraryInfo();
            openItineraryInfo.setArrCity("北京"); //到达城市
            openItineraryInfo.setArrCityCode("BJS"); //可选：到达城市编码

            openItineraryInfo.setDepCity("杭州"); //到达城市
            openItineraryInfo.setDepCityCode("HGH");  //可选：出发城市编码
            openItineraryInfo.setTrafficType(0L);  //交通方式：0飞机, 1,火车, 2汽车, 3其他
            openItineraryInfo.setItineraryId("金螳螂行程单itineraryId" + new Random().nextInt(99999));
            openItineraryInfo.setTripWay(1L); //行程类型：0单程，1往返
            openItineraryInfo.setArrDate(StringUtils.parseDateTime("2018-01-19 00:00:00")); //到达日期
            openItineraryInfo.setDepDate(StringUtils.parseDateTime("2018-01-17 00:00:00")); //出发日期
            openItineraryInfo.setInvoiceId(4969L); //苏州金螳螂建筑装饰股份有限公司
            openItineraryInfo.setCostCenterId(3046L);  //可选：苏州金螳螂建筑装饰股份有限公司
            openItineraryInfo.setThirdpartCostCenterId("第三方成本中心id，若不填则商旅成本中心id必填");  //第三方成本中心id，若不填则商旅成本中心id必填
            itineraryInfoList.add(openItineraryInfo);


            //出行人列表
            List<AlitripBtripApprovalNewRequest.OpenUserInfo> travelerList = new ArrayList<>();
            AlitripBtripApprovalNewRequest.OpenUserInfo userInfo = new AlitripBtripApprovalNewRequest.OpenUserInfo();
              userInfo.setUserId("39688");
            userInfo.setUserName("陈金东");
            travelerList.add(userInfo);

            AlitripBtripApprovalNewRequest.OpenUserInfo userInfo2 = new AlitripBtripApprovalNewRequest.OpenUserInfo();
            userInfo2.setUserId("33096");
            userInfo2.setUserName("宋黎阳");
            travelerList.add(userInfo2);

            //初始化行程单
            AlitripBtripApprovalNewRequest.OpenAddApplyRq applyRequest = new AlitripBtripApprovalNewRequest.OpenAddApplyRq();
            applyRequest.setTripTitle("本周需要从苏州出差到北京，外出两天"+ new Date());
            applyRequest.setApplyId("金螳螂申请单applyId:" + new Date());

            // 出差天数
            applyRequest.setTripDay(2L);

            applyRequest.setDepartId("13125050");
            applyRequest.setDepartName("信息中心研发部"); //optional
            applyRequest.setUserId("39688");
            applyRequest.setUserName("陈金东");
            applyRequest.setTripCause("项目交接");

            applyRequest.setItineraryList(itineraryInfoList);
            applyRequest.setTravelerList(travelerList);


            long start = System.currentTimeMillis();

            AlitripBtripApprovalModel rsp = approvalNewService.insertNewApproval(applyRequest, itineraryInfoList, travelerList);

            long end = System.currentTimeMillis();

            System.out.println(end-start);
            if (logger.isInfoEnabled()) {
                System.out.println(JSON.toJSONString(rsp));
                logger.info(applyRequest.getApplyId() + "单据同步成功:" + JSON.toJSONString(rsp));
            }
            return ResponseEntity.ok(rsp);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("新增出错，item=" + new Object() + "/r/n错误信息：" + e.getStackTrace());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
